<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>自定义表</title>
        <link rel="stylesheet" href="${ctx}/css/font/iconfontie.css" media="all"/>
        <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
        <#include "/pubHeadStyleTree.html">
    </head>
    <body>
        <div style="position: relative;height: 100%;">
            <div class="ccpui-trees">
                <p class="isShowTree colorSwitch">
                    <span style="float: left;color: #fff">&nbsp;&nbsp;表分类管理</span>
                    <i layadmin-event="treeFlexible" class="layui-icon layui-icon-prev"></i>
                </p>
                <div style="margin-top: 6px;">
                    <ul class="ccpui-iconTree iconTrees">
                        <li><a class="iconfont iconshuaxin- zTree-refresh" title="刷新"></a></li>
                        <li><a class="iconfont iconwenjianjia1-copy zTree-triangle-d" title="展开"></a></li>
                        <li><a class="iconfont iconfolder1 zTree-triangle-r" title="收起"></a></li>
                        <input type="text" style="width: 50%" class="tableZtreeValue">
                    </ul>
                    <ul id="treeClass" class="ztree ccpui-listZtree"></ul>
                </div>
            </div>
            <div class="ccpui-coreContent formView">
                <table id="customTable" lay-filter="customTable" width="100%"></table>
            </div>
        </div>
        <div class="rightClick">
            <div id="classRightMenus">
                <ul>
                    <li id="addNode">增加节点</li>
                    <li id="editNode">修改节点</li>
                    <li id="removeNode">删除节点</li>
                </ul>
            </div>
        </div>
        <script type="text/html" id="customHeaderTool">
            <div class="ccpui-enterSel">
                <div class="layui-btn-container">
                    <a class="layui-btn layui-btn-sm" lay-event="query">
                        <i class="layui-icon layui-icon-search"></i>
                        <span>查询</span>
                    </a>
                    <a class="layui-btn layui-btn-sm" lay-event="add">
                        <i class="layui-icon layui-icon-add-1"></i>
                        <span>添加</span>
                    </a>
                    <a class="layui-btn layui-btn-sm" lay-event="del">
                        <i class="layui-icon layui-icon-delete"></i>
                        <span>删除</span>
                    </a>
                    <a class="layui-btn layui-btn-sm" lay-event="addTable">
                        <i class="layui-icon layui-icon-add-1"></i>
                        <span>添加外部表</span>
                    </a>
                    <a class="layui-btn layui-btn-sm" lay-event="export">
                        <i class="layui-icon layui-icon-export"></i>
                        <span>导出</span>
                    </a>
                    <a class="layui-btn layui-btn-sm" lay-event="import">
                        <i class="layui-icon layui-icon-download-circle"></i>
                        <span>导入</span>
                    </a>
                    <a class="layui-btn layui-btn-sm" lay-event="reset">
                        <i class="layui-icon layui-icon-refresh"></i>
                        <span>重置</span>
                    </a>
                </div>
                <form class="layui-form" action="" style="margin-top:10px" lay-filter="table">
                    <div class="table-top-filter">
                        <div class="table-filter-alone">
                            <label class="layui-form-label">表名</label>
                            <input type="text" name="Q_tableName_SL" id="tableName" class="layui-input filter-value"
                                   placeholder="表名"
                                   autocomplete="off"/>
                        </div>
                        <div class="table-filter-alone">
                            <label class="layui-form-label">注释</label>
                            <input type="text" name="Q_tableDesc_SL" id="tableDesc" class="layui-input filter-value"
                                   placeholder="注释"
                                   autocomplete="off"/>
                        </div>
                        <div class="table-filter-alone">
                            <label class="layui-form-label">是否主表</label>
                            <select name="Q_isMain_S" id="isMain" class="filter-value">
                                <option value=""></option>
                                <option value="1">主表</option>
                                <option value="0">子表</option>
                            </select>
                        </div>
                        <div class="table-filter-alone">
                            <label class="layui-form-label">生成方式</label>
                            <select name="Q_genByForm_S" id="genByForm" class="filter-value">
                                <option value="">全部</option>
                                <option value="0">自定义表</option>
                                <option value="1">由表单生成</option>
                            </select>
                        </div>
                        <div class="table-filter-alone">
                            <label class="layui-form-label">是否生成</label>
                            <select name="Q_isPublished_S" id="isPublished" class="filter-value">
                                <option value="">全部</option>
                                <option value="0">未生成</option>
                                <option value="1">已生成</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
        </script>
        <script type="text/html" id="operation">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            {{# if(d.isExternal == 0 && d.isPublished == 1){ }}
            <a class="layui-btn layui-btn-xs" lay-event="reset">重置</a>
            {{# }}}
            {{# if(d.isPublished == 0){ }}
            <a class="layui-btn layui-btn-xs" lay-event="generateTable">生成表</a>
            {{# }}}
            <a class="layui-btn layui-btn-xs" lay-event="tableDetail">明细</a>
            {{# if(d.isMain == 1){ }}
            <a class="layui-btn layui-btn-xs" lay-event="copy">复制</a>
            {{# }}}
        </script>

        <script>
            layui.config({
                base: serverPath.formPath + staticPath.formPath //静态资源所在路径
            }).extend({
                index: 'index' //主入口模块
            }).use(['index', 'setter', 'commons', 'form', 'jquery', 'layer', 'common', 'table', 'totalFilter'], function () {
                var table = layui.table, layer = layui.layer, setter = layui.setter, common = layui.common,
                    totalFilter = layui.totalFilter,commons = layui.commons

                common.tableSort("customTable");
                //刷新
                $(".zTree-refresh").click(function () {
                    resZtrees();
                });
                //展开
                $(".zTree-triangle-d").click(function () {
                    $.fn.zTree.getZTreeObj("treeClass").expandAll(true);
                });
                //收起
                $(".zTree-triangle-r").click(function () {
                    $.fn.zTree.getZTreeObj("treeClass").expandAll(false);
                });
                resZtrees();
                loadTable(serverPath.formPath + setter.formLinkPath + 'table/list');

                function resZtrees() {
                    $.ajax({
                        url: serverPath.formPath + setter.sysLinkPath + "globalType/list",
                        type: "post",
                        dataType: 'json',
                        data: {"catKey": "TABLE_TYPE", "hasRoot": 1},
                        success: function (data) {
                            var zNodes = data.data;
                            var setting = {
                                view: {
                                    showLine: false,
                                    nameIsHTML: true, //允许name支持html
                                    selectedMulti: false
                                },
                                edit: {
                                    enable: false,
                                    editNameSelectAll: false
                                },
                                data: {
                                    simpleData: {
                                        enable: true,
                                        idKey: "typeId",
                                        pIdKey: "parentId"

                                    },
                                    key: {
                                        name: "typeName"
                                    }
                                },
                                callback: {
                                    onClick: onClickType,
                                    onRightClick: OnRightClickres
                                }
                            };
                            $.fn.zTree.init($('#treeClass'), setting, zNodes);
                            component.fuzzySearch('treeClass', '.tableZtreeValue', null, true); //初始化模糊搜索方法
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.alert("暂无数据，请稍后操作！" + XMLHttpRequest.status);
                        }
                    });
                }

                //右键菜单
                function OnRightClickres(event, treeId, treeNode) {
                    if (treeNode && !treeNode.noR) {
                        ztreeNode = treeNode;
                        selectResId = treeNode.typeId;
                        parentIds = treeNode.parentId;
                        showRMenus(event.clientX, event.clientY);
                    }
                }

                var categoryId = "";

                //树的单击
                function onClickType(event, treeId, treeNode) {
                    categoryId = treeNode.typeId;
                    var url = serverPath.formPath + setter.formLinkPath + 'table/list?categoryId=' + categoryId;
                    if (null == treeNode.parentId) {
                        url = serverPath.formPath + setter.formLinkPath + 'table/list'
                    }
                    loadTable(url);
                }

                //显示右键菜单
                function showRMenus(x, y) {
                    $("#classRightMenus ul").show();
                    var h = $('#classRightMenus').height()
                    x += 15;
                    y += 10;
                    if ($(window).height() < y + h) (
                        y = y - h
                    )
                    $("#classRightMenus").css({"top": y + "px", "left": x + "px", "visibility": "visible"});

                    $("body").bind("mousedown", onBodyMouseDown);
                }

                function onClickRess(event, treeId, treeNode, clickFlag) {
                    selectOrgId = treeNode.orgId;
                }

                function hideRMenu() {
                    if ($("#classRightMenus")) $("#classRightMenus").css({"visibility": "hidden"});
                    $("body").unbind("mousedown", onBodyMouseDown);
                }

                function onBodyMouseDown(event) {
                    if (!(event.target.id == "orgRightMenu" || $(event.target).parents("#classRightMenus").length > 0)) {
                        $("#classRightMenus").css({"visibility": "hidden"});
                    }
                }

                //增加节点弹窗
                $("#addNode").click(function () {
                    var isRoot;
                    if (ztreeNode.parentId == null) {
                        isRoot = 1;//根节点
                    } else {
                        isRoot = 0;
                    }
                    layer.open({
                        type: 2,
                        area: ['440px', '280px'],
                        title: ['新增节点'],
                        content: serverPath.formPath + setter.sysLinkPath + "globalType/view/addNode" + '?isRoot=' + isRoot + '&parentId=' + selectResId
                        , end: function () {
                            resZtrees();
                        }
                    });
                });
                //修改节点弹窗
                $("#editNode").click(function () {
                    if (ztreeNode.parentId == null) {
                        layer.alert("根节点不能修改");
                    } else {
                        layer.open({
                            type: 2,
                            area: ['440px', '280px'],
                            title: ['修改节点'],
                            content: serverPath.formPath + setter.sysLinkPath + "globalType/view/addNode" + '?typeId=' + selectResId
                            , end: function () {
                                resZtrees();
                            }
                        });
                    }
                });
                //删除节点
                $("#removeNode").click(function () {
                    if (ztreeNode.parentId == null) {
                        layer.alert("根节点不能删除");
                    } else {
                        layer.confirm('确定删除当前节点吗？', function (index) {
                            layer.close(index);
                            $.ajax({
                                url: serverPath.formPath + setter.sysLinkPath + 'globalType/del',
                                type: "post",
                                dataType: "json",
                                data: {"typeId": selectResId},
                                success: function (result) {
                                    layer.alert(result.msg);
                                    var zTree = $.fn.zTree.getZTreeObj("treeClass");
                                    var idArr = selectResId.split(",");
                                    $.each(idArr, function (i, value) {
                                        var node = zTree.getNodeByParam("typeId", value);
                                        zTree.removeNode(node);
                                    })
                                }
                            });
                        });
                    }
                });

                function loadTable(url) {
                    var tableH = commons.tabHeight();
                    table.render({
                        elem: '#customTable'
                        , url: url
                        , title: '自定义表'
                        , height: tableH
                        , defaultToolbar: []
                        , autoSort: false
                        , toolbar: '#customHeaderTool'
                        , cols: [[
                            {type: 'checkbox'}
                            , {field: 'tableName', title: '表名', sort: true, align: 'center'}
                            , {field: 'tableDesc', title: '注释', sort: true, align: 'center'}
                            , {
                                field: 'isMain', title: '属性', sort: true, align: 'center',
                                templet: function (d) {
                                    if (d.isMain == 1) {
                                        return '主表';
                                    }
                                    return '子表';
                                }
                            }
                            , {
                                field: 'isPublished', title: '状态', sort: true, align: 'center',
                                templet: function (d) {
                                    if (d.isPublished == 0) {
                                        return '<span style="color:red;">' + '未生成' + '</span>';

                                    }
                                    return '<span style="color:green;">' + '已生成' + '</span>';
                                }
                            }
                            , {field: 'dsAlias', title: '数据源别名', sort: true, align: 'center'}
                            , {
                                field: 'genByForm', title: '生成方式', sort: true, align: 'center',
                                templet: function (d) {
                                    if (d.genByForm == 0) {
                                        return '自定义表';
                                    }
                                    return '由表单生成';
                                }
                            }
                            , {field: 'subStr', title: '对应的子表', align: 'center'}
                            , {fixed: 'right', title: '管理', toolbar: '#operation', width: 150, align: 'center'}
                        ]]
                        , page: true
                        , done: function () {
                            common.tableSelectCon('.ccpui-enterSel .layui-form input', 'a[lay-event=query]', '.ccpui-enterSel')
                            table.resize();
                        }
                    });
                    totalFilter.init('customTable');
                }

                //头工具栏事件
                table.on('toolbar(customTable)', function (obj) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data; //获取选中行数据
                    switch (obj.event) {
                        //自定义表导出
                        case 'export':
                            //判断是否都是子表
                            var flag = true;
                            for (var i = 0; i < data.length; i++) {
                                if (data[i].isMain == 1) {
                                    flag = false;
                                    break;
                                }
                            }

                            var ids = [];
                            for (var j = 0; j < data.length; j++) {
                                if (data[j].isMain == 1) {
                                    ids.push(data[j].tableId);
                                }
                            }
                            var tableIds = ids.join(',');

                            if (data.length == 0 || flag) { //都是子表或者没有选择 都要提示
                                layer.confirm('请选择主表进行导出', {
                                    btn: ['确定'] //按钮
                                });
                            } else {
                                layer.open({
                                    type: 2,
                                    title: "导出自定义表",
                                    shade: 0.3,
                                    area: ['453px', '220px'],
                                    content: serverPath.formPath + setter.formLinkPath + 'table/view/export?tableIds=' + tableIds
                                })
                            }
                            break;
                        //自定义表导入
                        case 'import':
                            layer.open({
                                type: 2,
                                title: "导入自定义表",
                                shade: 0.3,
                                area: ['700px', '500px'],
                                content: serverPath.formPath + setter.formLinkPath + 'table/view/import'
                            });
                            break;
                        case 'query':
                            common.tableConditionRender("customTable", "table", {where: {}}, false);
                            break;
                        case 'add':
                            layer.open({
                                type: 2,
                                title: "添加",
                                shade: 0.3,
                                left: 200,
                                area: ['1000px', '550px'],
                                content: serverPath.formPath + setter.formLinkPath + "table/view/add?categoryId=" + categoryId
                            });
                            break;
                        case 'reset':
                            common.tableConditionRender("customTable", "table", {where: {}}, true);
                            break;
                        case 'del':
                            if (data.length === 0) {
                                layer.confirm('请选择记录！', {
                                    btn: ['确定'] //按钮
                                }, function (index) {
                                    layer.close(index);
                                });
                            } else {
                                layer.confirm('确定要删除选中的数据吗?', function (index) {
                                    var ids = [];
                                    for (var i = 0; i < data.length; i++) {
                                        ids.push(data[i].tableId);
                                    }
                                    var tableIds = ids.join(',');
                                    $.ajax({
                                        url: serverPath.formPath + setter.formLinkPath + "table/delByTableId",
                                        dataType: 'json',
                                        data: {"tableIds": tableIds},
                                        success: function (data) {
                                            if (null == data.data) {
                                                layer.alert(data.msg);
                                            } else {
                                                layer.alert(data.data + data.msg);
                                            }
                                            //执行重载
                                            table.reload('customTable', {
                                                page: {
                                                    curr: 1
                                                }
                                            });
                                            totalFilter.init('customTable');
                                        }
                                    })
                                });
                            }
                            break;
                        case 'addTable':
                            layer.open({
                                type: 2,
                                title: "添加外部表",
                                shade: 0.3,
                                area: ['1000px', '620px'],
                                content: serverPath.formPath + setter.formLinkPath + 'table/view/addExternalTable'
                            });
                            break;
                    }
                });

                //监听行工具事件
                table.on('tool(customTable)', function (obj) {
                    var tableId = obj.data.tableId;
                    switch (obj.event) {
                        case 'del':
                            var isMain = obj.data.isMain;
                            var delWarnStr = "确定要删除该表和其关联的子表吗?";
                            if(isMain===0){
                                delWarnStr = "确定要删除该表吗?";
                            }
                            layer.confirm(delWarnStr, function (index) {
                                var tableId = obj.data.tableId;
                                $.ajax({
                                    url: serverPath.formPath + setter.formLinkPath + "table/delByTableId",
                                    dataType: 'json',
                                    data: {"tableIds": tableId},
                                    success: function (data) {
                                        if (null == data.data) {
                                            layer.alert(data.msg);
                                        } else {
                                            layer.alert(data.data + data.msg);
                                        }
                                        //执行重载
                                        table.reload('customTable', {
                                            page: {
                                                curr: 1
                                            }
                                        });
                                        totalFilter.init('customTable');
                                    }
                                })
                            });
                            break;
                        case 'edit':
                            layer.open({
                                type: 2,
                                title: "编辑",
                                content: serverPath.formPath + setter.formLinkPath + 'table/view/add?tableId=' + tableId + '&isExternal=' + obj.data.isExternal,
                                area: ['1000px', '585px']
                            });
                            break;
                        case 'copy':
                            layer.open({
                                type: 2,
                                title: "复制",
                                shade: 0.3,
                                area: ['660px', '300px'],
                                content: serverPath.formPath + setter.formLinkPath + 'table/view/copy?tableId=' + tableId
                            });
                            break;
                        case 'tableDetail':
                            layer.open({
                                type: 2,
                                title: "明细",
                                shade: 0.3,
                                area: ['1000px', '585px'],
                                content: serverPath.formPath + setter.formLinkPath + 'table/view/detailed?tableId=' + tableId
                            });
                            break;
                        case 'grouping':
                            layer.open({
                                type: 2,
                                title: "分组",
                                shade: 0.3,
                                area: ['800px', '500px'],
                                content: serverPath.formPath + setter.formLinkPath + 'table/view/grouping?tableId=' + tableId
                                , end: function () {
                                    table.reload('customTable', {
                                        page: {
                                            curr: 1
                                        }
                                    });
                                    totalFilter.init('customTable');
                                }
                            });
                            break;
                        case 'reset':
                            layer.confirm('重置操作将删除物理表，并重置表的状态为未生成。是否重置该表?', function (index) {
                                var tableId = obj.data.tableId;
                                $.ajax({
                                    url: serverPath.formPath + setter.formLinkPath + "table/reset",
                                    dataType: 'json',
                                    data: {"tableId": tableId},
                                    success: function (data) {
                                        layer.alert(data.msg);
                                        //执行重载
                                        table.reload('customTable', {
                                            page: {
                                                curr: 1
                                            }
                                        });
                                        totalFilter.init('customTable');
                                    }
                                })
                            });
                            break;
                        case 'generateTable':
                            var isMain = obj.data.isMain;
                            var warnStr = "将连同子表一起生成,是否继续?";
                            if(isMain===0){
                                warnStr = "确定要生成该表吗?";
                            }
                            layer.confirm(warnStr, function (index) {
                                var tableId = obj.data.tableId;
                                $.ajax({
                                    url: serverPath.formPath + setter.formLinkPath + "table/generateTable",
                                    dataType: 'json',
                                    data: {"tableId": tableId},
                                    success: function (data) {
                                        layer.alert(data.msg);
                                        table.reload('customTable', {
                                            page: {
                                                curr: 1
                                            }
                                        });
                                        totalFilter.init('customTable');
                                    }
                                })
                            });
                            break;
                    }
                });
            });
        </script>
        <style>
            .rightClick #classRightMenus {
                position: fixed;
                border: 1px solid #ccc;
                z-index: 900;
                visibility: hidden;
                top: 0;
                background-color: #f5f5f5;
                text-align: left;
                padding: 2px;
            }

            .rightClick #classRightMenus ul li {
                margin: 5px 0;
                padding: 0 8px;
                cursor: pointer;
                list-style: none outside none;
            }
        </style>
    </body>
</html>
<link rel="stylesheet" href="${ctx}/css/font/iconfontie.css" media="all"/>
<link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>